package com.lou.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author : ljl
 * @date 创建时间：2018年9月13日 下午4:57:25
 * @version 1.0
 * @parameter
 * @since
 * @return
 */
public class Crawler {
	public static void main(String[] args) {
		String r;
		// 1.新建url对象，表示要访问的网页
		try {
			URL url = new URL("https://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=result&fr=&sf=1&fmq=1536828952787_R&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&word=%E8%BF%AA%E8%BF%A6");
			// 2.建立http连接,返回连接对象urlconnection
			HttpURLConnection urlConnection = (HttpURLConnection) url
					.openConnection();
			// 3.获取相应的http状态码，
			int responseCode = urlConnection.getResponseCode();
			// 4.如果获取成功，从URLconnection对象获取输入流来获取请求网页的源代码
			if (responseCode == 200) {
				BufferedReader reader = new BufferedReader(
						new InputStreamReader(urlConnection.getInputStream(),
								"utf-8"));
				while ((r = reader.readLine()) != null) {
					System.out.println(r);
				}
			} else {
				System.out.println("获取不到源代码 ，服务器响应代码为：" + responseCode);
			}
		} catch (Exception e) {
			System.out.println("获取不到网页源码：" + e);
		}
	}
}
